// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <benchmark/benchmark.h>

#include <cstdint>
#include <vector>

#include <nebula/array/array_base.h>
#include <nebula/testing/ktest_util.h>
#include <nebula/testing/random.h>
#include <nebula/util/benchmark_util.h>
#include <nebula/numeric/int_util.h>

namespace nebula {
namespace internal {

constexpr auto kSeed = 0x94378165;

std::vector<uint64_t> GetUIntSequence(int n_values, uint64_t addend = 0) {
  std::vector<uint64_t> values(n_values);
  for (int i = 0; i < n_values; ++i) {
    values[i] = static_cast<uint64_t>(i) + addend;
  }
  return values;
}

std::vector<int64_t> GetIntSequence(int n_values, uint64_t addend = 0) {
  std::vector<int64_t> values(n_values);
  for (int i = 0; i < n_values; ++i) {
    values[i] = static_cast<int64_t>(i) + addend;
  }
  return values;
}

std::vector<uint8_t> GetValidBytes(int n_values) {
  std::vector<uint8_t> valid_bytes(n_values);
  for (int i = 0; i < n_values; ++i) {
    valid_bytes[i] = (i % 3 == 0) ? 1 : 0;
  }
  return valid_bytes;
}

static void DetectUIntWidthNoNulls(
    benchmark::State& state) {  // NOLINT non-const reference
  const auto values = GetUIntSequence(0x12345);

  while (state.KeepRunning()) {
    auto result = DetectUIntWidth(values.data(), static_cast<int64_t>(values.size()));
    benchmark::DoNotOptimize(result);
  }
  state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
  state.SetItemsProcessed(state.iterations() * values.size());
}

static void DetectUIntWidthNulls(benchmark::State& state) {  // NOLINT non-const reference
  const auto values = GetUIntSequence(0x12345);
  const auto valid_bytes = GetValidBytes(0x12345);

  while (state.KeepRunning()) {
    auto result = DetectUIntWidth(values.data(), valid_bytes.data(),
                                  static_cast<int64_t>(values.size()));
    benchmark::DoNotOptimize(result);
  }
  state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
  state.SetItemsProcessed(state.iterations() * values.size());
}

static void DetectIntWidthNoNulls(
    benchmark::State& state) {  // NOLINT non-const reference
  const auto values = GetIntSequence(0x12345, -0x1234);

  while (state.KeepRunning()) {
    auto result = DetectIntWidth(values.data(), static_cast<int64_t>(values.size()));
    benchmark::DoNotOptimize(result);
  }
  state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
  state.SetItemsProcessed(state.iterations() * values.size());
}

static void DetectIntWidthNulls(benchmark::State& state) {  // NOLINT non-const reference
  const auto values = GetIntSequence(0x12345, -0x1234);
  const auto valid_bytes = GetValidBytes(0x12345);

  while (state.KeepRunning()) {
    auto result = DetectIntWidth(values.data(), valid_bytes.data(),
                                 static_cast<int64_t>(values.size()));
    benchmark::DoNotOptimize(result);
  }
  state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
  state.SetItemsProcessed(state.iterations() * values.size());
}

static void CheckIndexBoundsInt32(
    benchmark::State& state) {  // NOLINT non-const reference
  GenericItemsArgs args(state);
  random::RandomArrayGenerator rand(kSeed);
  auto arr = rand.Int32(args.size, 0, 100000, args.null_proportion);
  for (auto _ : state) {
    ABORT_NOT_OK(CheckIndexBounds(*arr->data(), 100001));
  }
}

static void CheckIndexBoundsUInt32(
    benchmark::State& state) {  // NOLINT non-const reference
  GenericItemsArgs args(state);
  random::RandomArrayGenerator rand(kSeed);
  auto arr = rand.UInt32(args.size, 0, 100000, args.null_proportion);
  for (auto _ : state) {
    ABORT_NOT_OK(CheckIndexBounds(*arr->data(), 100001));
  }
}

BENCHMARK(DetectUIntWidthNoNulls);
BENCHMARK(DetectUIntWidthNulls);
BENCHMARK(DetectIntWidthNoNulls);
BENCHMARK(DetectIntWidthNulls);

std::vector<int64_t> g_data_sizes = {kL1Size, kL2Size};

void BoundsCheckSetArgs(benchmark::internal::Benchmark* bench) {
  for (int64_t size : g_data_sizes) {
    for (auto nulls : std::vector<ArgsType>({1000, 10, 2, 1, 0})) {
      bench->Args({static_cast<ArgsType>(size), nulls});
    }
  }
}

BENCHMARK(CheckIndexBoundsInt32)->Apply(BoundsCheckSetArgs);
BENCHMARK(CheckIndexBoundsUInt32)->Apply(BoundsCheckSetArgs);

}  // namespace internal
}  // namespace nebula
